/**
 *  easyUI验证扩展，增加自定义验证2014-5-29 15:16:07
 * 
 * 使用方法: 单个验证: data-options="validType:'minLength[5]'"  或者  data-options="[validType:'minLength[5]']" 
 *			 多个验证: data-options="validType:['isNull','unsafe','minLength[5]']"
 * 
 * @param {Object} value
 * @param {Object} param
 * @return {TypeName} 
 */
$.extend($.fn.validatebox.defaults.rules, {
	isNull: { // 非空判断，主要是针对输入空格的验证
		validator: function(value, param){
			if(value.indexOf(' ') != -1){
				return false;
			}
			return true;
		},
		message: '该项不能输入空格'
	},
	airportCode: { // 验证机场三字码为大写英文字母
		validator: function(value,param){
			return /^[A-Z]+$/.test(value);
		},
		message: '请输入大写英文字母'
	},
	minLength: { // 最小长度验证  使用：data-options="validType:['minLength[3]']"
		validator: function(value, param){
			return value.length >= param[0];
		},
		message: '不能少于{0}个字符'
	},
	maxLength: { // 最大长度验证  使用：data-options="validType:['maxLength[10]']"
		validator: function(value, param){
			return value.length <= param[0];
		},
		message: '不能超过{0}个字符'
	},
	unsafe: { // 非法字符验证
		validator: function(value, param){
			return !/[\\\/:\*#$\?？\<\>\|\^%\@\~\￥\{\}\(\),\'‘’“”，。\+\、《》；：！（）;.!&\"]/.test(value);
		},
		message: '该项不能输入非法字符'
	},
	uneasysafe: { // 简单非法字符验证
		validator: function(value, param){
			return !/[\\\/:\*#$\?？\<\>\|\^%\@\~\￥\{\}\(\)\'‘’“”。\+\《》；：！（）;.!&\"]/.test(value);
		},
		message: '该项不能输入非法字符'
	},
	number:{//数字 add by lif
		validator: function(value, param){
			return /^-?\d+$/.test(value);
		},
		message: '请输入正确的数字'
	},
	double:{//double类型校验
		validator: function(value, param){
			return /^\d+|\d+\.\d{1,2}$/.test(value);
		},
		message: '请输入正确的数据格式'
	},
	isIp:{// add by shenb
		validator: function(value, param){
			return /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(value);
		},
		message: '请输入正确的IP'
	},
	unique: {    
	        validator: function(value,param){
	        	if(param[3] == undefined){
	        		param[3]='';
	        		if(opBusseness != undefined){
	        			oldValue = '';
	        			oldResult = true;
	        			opBusseness = undefined;
	        		}
	        	}else{
	        		if(opBusseness != param[3]){
	        			oldValue = '';
	        			oldResult = true;
	        			opBusseness = param[3];
	        		}
	        	}
	        	value = encodeURI(encodeURI(value.replace(/\+/g," "),true));
	        	var result = oldResult;
	        	if($.trim(value) != '' && $.trim(value) != oldValue){
	        		$.ajax({
						type: "get",
						async:false,
						url:  param[0]+"/validate/unique",
						data:'tableName='+param[1]+'&field='+param[2]+'&value='+value+'&id='+param[3],
						success: function(data){
							if(data == "success"){
								result = true;
							}else {
								result = false;
							}
						}
					});
	        	}
	        	oldValue = $.trim(value);
	        	oldResult = result;
				return result;
	        },    
	        message: '命名重复，请重新命名'   
	    },
	email:{// add by wangb
		validator: function(value, param){
			return /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(value);
		},
		message: '请输入正确的邮箱'
	},
	date:{// add by fengya
		validator: function(value, param){
			return /\d{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(3[0-1]))$/.test(value);
		},
		message: '请输入正确格式的日期'
	},
	card:{// add by fengya
		validator: function(value, param){
			return /(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(value);
		},
		message: '请输入正确格式的身份证号码'
	},
	charAndDigit:{// add by fengya
		validator: function(value, param){
			return /(^[a-zA-Z0-9]+$)/.test(value);
		},
		message: '该项只能输入字母和数字'
	},
	phone:{// add by wangb
		validator: function(value, param){
			return /^((\(\d{2,3}\))|(\d{3}))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/.test(value);
		},
		message: '请输入正确的电话号码'
	},
	mobile:{// add by wangb
		validator: function(value, param){
			return /^1[3|4|5|8][0-9]\d{8}$/.test(value);
		},
		message: '请输入正确的手机号'
	},
	mobileOrPhone:{// add by wangb
		validator: function(value, param){
			return /^1[3|4|5|8][0-9]\d{8}$/.test(value) || /^((\(\d{2,3}\))|(\d{3}))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/.test(value);
		},
		message: '请输入正确的联系电话'
	},
	maxNumber:{ // add by zhoulh 用于绩效考核-评分管理-验证分数最大值
		validator: function(value, param){
			return value <= param[0];
		},
		message: '输入值不能大于：{0}'
	},
	minNumber:{ // add by zhoulh 用于绩效考核-评分管理-验证分数最大值
		validator: function(value, param){
			return value >= param[0];
		},
		message: '输入值不能小于：{0}'
	},
	
	max: { //用法validType="max['fxpgtjbId']" add by wangb 开始值小于结束值
			validator: function(value,param){
			return parseInt(value) < parseInt($(document.getElementById(param[0])).val())
			},
			message: '开始值必须小于结束值'
		},
	min: { //用法validType="min['fxpgtjbId']" add by wangb 结束值大于开始值
			validator: function(value,param){
			return parseInt(value) > parseInt($(document.getElementById(param[0])).val())
			},
			message: '结束值必须大于开始值'
		},
	maxDate: { 
		validator: function(value,param){
			var dateValue = $.trim(value);
			if(dateValue != '' && param[0] != ''){
				var datestr = dateValue.replace(/-/g,"/");
				var date = new Date(datestr);
				return date.getTime()<param[0];
			}
			return false;
		},
		message: '时间不能大于当前时间'
	},
	minDate: { 
		validator: function(value,param){
			var dateValue = $.trim(value);
			if(dateValue != '' && param[0] != ''){
				var datestr = dateValue.replace(/-/g,"/");
				var date = new Date(datestr);
				return date.getTime()>param[0];
			}
			return false;
		},
		message: '时间不能小于当前时间'
	},
	validateNum:{ // add by zhoulh 小数位数验证，只能输入一位小数
		validator: function(value, param){
			return /^(\d+\.\d{1,1}|\d+)$/.test(value);
		},
		message: '请输入符合规则且小数位数为一位的数字'
	},
	validateAffirmPassword:{ // add by chirq 验证两次密码是否一致是否相等
		validator: function(value, param){
			return value == $('#'+param[0]).val();
		},
		message: '确认密码与新密码不一致'
	},
	chineseChar:{ // add by wangdingchun 中文类型
		validator: function(value){
			return /^[\u4e00-\u9fa5]+$/.test(value);
		},
		message: '必须为中文格式'
	},
	describe:{ // add by wangdingchun  描述、备注等要求的格式
		validator: function(value){
			return /^[\u4e00-\u9fa5A-Za-z\d\s'"]+$/.test(value);
		},
		message: '存在非法字符'
	},
	length:{ // add by wangdingchun  长度最小值和最大值
    	validator:function(value,param){
        var len=$.trim(value).length;
            return len>=param[0]&&len<=param[1];
        },
            message:"内容长度介于{0}和{1}之间."
    },
    defalutSelect:{
    	validator:function(value){
    		if(value=="请选择")
    			return false;
    		else
    			return true;
    	},
    		message:"该项值不能为空或默认值"
    },
    digitalFormat:{ //add by cuixing  验证分数必须为0.5倍数
    	validator:function(value){
    		return /(^[0-9]*[1-9][0-9]*$)/.test(value*2);
    	},
    		message:"分数必须为0.5的倍数"
    },
    positiveInteger:{ //add by cuixing  验证数字必须为正整数
    	validator:function(value){
    		return /(^[1-9]+[0-9]*$)/.test(value);
    	},
    		message:"必须为大于0的整数"
    },
    textVerification:{
    	validator:function(value){
    		return /(^[\u4e00-\u9fa5a-zA-Z0-9]+$)/.test(value);
    	},
    		message:"必须为中文、英文、数字"
    },
    url:{
    	validator:function(value){
    		return /(^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$)/.test(value);
    	},
    		message:"url格式"
    },
    dateTimeMoreCompare: { // add by fengya  日期比较(yyyy-MM-dd格式比较)
		validator: function(value,param){
			var beginTime = $.trim($('#'+param[0]).datetimebox('getValue'));
			if($.trim(beginTime)!=''){
				var beginDate = new Date(beginTime.replace(/-/g,"/"));
				var endDate = new Date($.trim(value).replace(/-/g,"/"));
				return endDate.getTime() >= beginDate.getTime();
			}
			return true;
		},
		message: '结束时间不能小于开始时间'
	},
    dateTimeLessCompare: { // add by fengya  日期比较(yyyy-MM-dd格式比较)
			validator: function(value,param){
				var endDate = $.trim($('#'+param[0]).datetimebox('getValue'));
				if($.trim(endDate)!=''){
					var endDate = new Date(endDate.replace(/-/g,"/"));
					var beginDate = new Date($.trim(value).replace(/-/g,"/"));
					return endDate.getTime() >= beginDate.getTime();
				}
				return true;
			},
			message: '开始时间不能大于结束时间'
		}
});
var oldValue = '';
var oldResult = true;
var opBusseness;